﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

using QuickDev.Model;
using QuickDev.Common.DB;
using QuickDev.Common.Utils;

#endregion

namespace QuickDev.SQLServerDAL
{
    /// <summary>
    /// Data Access Layer For dbo.SYS_Provider.
    /// </summary>
    public class ProviderDAL
    {
        #region IsExist

        /// <summary>
        /// public static bool IsExist( int providerTypeId , string name )
        /// </summary>
        /// <param name="providerTypeId">providerTypeId</param>
        /// <param name="name">name</param>
        /// <returns>bool Existed</returns>
        public static bool IsExist( int providerTypeId, string name )
        {
            string procedureName = "SYS_Provider_IsExist";

            SqlParameter[] parameters = {
					new SqlParameter("@ProviderTypeId", SqlDbType.Int,4),
                    new SqlParameter("@Name", SqlDbType.VarChar,50),
                    new SqlParameter()
				};
            parameters[0].Value = providerTypeId;
            parameters[1].Value = name;
            parameters[2].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );
                return ( (int)( parameters[2].Value ) == 0 ? false : true );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }

        #endregion

        #region GetModel

        /// <summary>
        /// public Provider GetModel(int providerId)
        /// </summary>
        /// <param name="providerId">providerId</param>
        /// <returns>Provider model</returns>
        public static Provider GetModel( int providerId )
        {
            string procedureName = "SYS_Provider_GetModel";

            Provider model = new Provider();

            SqlParameter parameter = new SqlParameter( "@ProviderId", providerId );

            try
            {
                ReaderFactory.ReaderToModel<Provider>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure, procedureName, parameter ), model );
                return model;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region GetList

        /// <summary>
        /// public List<Provider> GetList( int providerTypeId , int status )
        /// </summary>
        /// <param name="providerTypeId">providerTypeId</param>
        /// <param name="status">status</param>
        /// <returns>List<Provider></returns>
        public static List<Provider> GetList( int providerTypeId, int status )
        {
            string procedureName = "SYS_Provider_GetList";

            List<Provider> modelList = new List<Provider>();

            SqlParameter[] parameters = {
					new SqlParameter("@ProviderTypeId", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4)
				};
            parameters[0].Value = providerTypeId;
            parameters[1].Value = status;

            try
            {
                ReaderFactory.ReaderToList<Provider>( SqlDataCommon.ExecuteReader( CommandType.StoredProcedure, procedureName, parameters ), modelList );
                return modelList;
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region Add

        /// <summary>
        /// public static int Add( int providerTypeId, string name, string description, string configurationControlPath, int createBy )
        /// </summary>
        /// <param name="providerTypeId">providerTypeId</param>
        /// <param name="name">name</param>
        /// <param name="description">description</param>
        /// <param name="configurationControlPath">configurationControlPath</param>
        /// <param name="createBy">createBy</param>
        /// <returns>ProviderId</returns>
        public static int Add( int providerTypeId, string name, string description, string configurationControlPath, int createBy )
        {
            if ( IsExist( providerTypeId, name ) )
                throw new ArgumentException
                    ( String.Format( "Provider {0} for this product is already in use", name ) );

            string procedureName = "SYS_Provider_ADD";

            SqlParameter[] parameters = {
				new SqlParameter("@ProviderTypeId", SqlDbType.Int,4),	
				new SqlParameter("@Name", SqlDbType.VarChar,50),	
				new SqlParameter("@Description", SqlDbType.VarChar,8000),	
				new SqlParameter("@ConfigurationControlPath", SqlDbType.VarChar,200),	
				new SqlParameter("@CreateBy", SqlDbType.Int,4),
                new SqlParameter("@ProviderId", SqlDbType.Int,4)
			};

            parameters[0].Value = providerTypeId;
            parameters[1].Value = name;
            parameters[2].Value = description;
            parameters[3].Value = configurationControlPath;
            parameters[4].Value = createBy;
            parameters[5].Direction = ParameterDirection.Output;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );

                return (int)Utility.ChangeTypeTo(parameters[5].Value, typeof(Int32));
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region Update

        /// <summary>
        /// public static bool Update( int providerId, int providerTypeId, string name, string description, string configurationControlPath, int updateBy )
        /// </summary>
        /// <param name="providerId">providerId</param>
        /// <param name="providerTypeId">providerTypeId</param>
        /// <param name="name">name</param>
        /// <param name="description">description</param>
        /// <param name="configurationControlPath">configurationControlPath</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int providerId, int providerTypeId, string name, string description, string configurationControlPath, int updateBy )
        {
            string procedureName = "SYS_Provider_Update";

            SqlParameter[] parameters = {
				new SqlParameter("@ProviderId", SqlDbType.Int,4),	
				new SqlParameter("@ProviderTypeId", SqlDbType.Int,4),	
				new SqlParameter("@Name", SqlDbType.VarChar,50),	
				new SqlParameter("@Description", SqlDbType.VarChar,8000),	
				new SqlParameter("@ConfigurationControlPath", SqlDbType.VarChar,200),	
				new SqlParameter("@UpdateBy", SqlDbType.Int,4),
                new SqlParameter()
			};

            parameters[0].Value = providerId;
            parameters[1].Value = providerTypeId;
            parameters[2].Value = name;
            parameters[3].Value = description;
            parameters[4].Value = configurationControlPath;
            parameters[5].Value = updateBy;
            parameters[6].Direction = ParameterDirection.ReturnValue;

            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );

                return ( (int)( parameters[6].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion

        #region UpdateStatus

        /// <summary>
        /// public bool Update(int providerId, int status, int updateBy)
        /// </summary>
        /// <param name="providerId">providerId</param>
        /// <param name="status">status</param>
        /// <param name="updateBy">updateBy</param>
        /// <returns>bool successful</returns>
        public static bool Update( int providerId, int status, int updateBy )
        {
            string procedureName = "SYS_Provider_UpdateStatus";

            SqlParameter[] parameters = {
                new SqlParameter("@ProviderId", SqlDbType.Int, 4),
                new SqlParameter("@Status", SqlDbType.Int, 4),
                new SqlParameter("@UpdateBy", SqlDbType.Int, 4),
                new SqlParameter()
			};

            parameters[0].Value = providerId;
            parameters[1].Value = status;
            parameters[2].Value = updateBy;
            parameters[3].Direction = ParameterDirection.ReturnValue;
            try
            {
                SqlDataCommon.ExecuteNonQuery( CommandType.StoredProcedure, procedureName, parameters );

                return ( (int)( parameters[3].Value ) == 0 ? true : false );
            }
            catch ( System.Exception ex )
            {
                throw new ApplicationException( "An error has occurred in accessing the database", ex );
            }
        }
        #endregion
    }
}

